package com.cswangdao.service.impl;

import com.cswangdao.domain.po.DayUsers;
import com.cswangdao.domain.vo.*;
import com.cswangdao.mapper.MarketStatisticMapper;
import com.cswangdao.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private MarketStatisticMapper statisticMapper;


    /**
     * @description: 统计每日新增用户
     * @param: null
     * @return: com.cswangdao.domain.vo.StatUsers
     */
    @Override
    public StatUsers statUsers() {
        try/*(SqlSession sqlSession = MybatisUtil.getSqlSession())*/ {
        //    MarketStatisticMapper statisticMapper = sqlSession.getMapper(MarketStatisticMapper.class);
            List<DayUsers> dayUsers = statisticMapper.statusUsers();
            for (DayUsers dayUser : dayUsers) {
                if(Objects.isNull(dayUser.getDay()))
                    dayUser.setDay("其它");
            }
            StatUsers statUsers = new StatUsers();
            statUsers.setRows(dayUsers);
            Field[] declaredFields = DayUsers.class.getDeclaredFields();
            List<String> columns = new ArrayList<>();
            for (Field declaredField : declaredFields) {
                columns.add(declaredField.getName());
            }
            statUsers.setColumns(columns);
            return statUsers;
        } catch (SecurityException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @description: 首页数据统计
     * @param: null
     * @return: DashboardVo
     */
    @Override
    public DashboardVo getDashboard() {
        try/*(SqlSession sqlSession = MybatisUtil.getSqlSession()) */{
     //       MarketStatisticMapper statisticMapper = sqlSession.getMapper(MarketStatisticMapper.class);

            Long goodsTotal = statisticMapper.getGoodsTotal();
            Long orderTotal = statisticMapper.getOrderTotal();
            Long productTotal = statisticMapper.getProductTotal();
            Long userTotal = statisticMapper.getUserTotal();
            DashboardVo dashboardVo = DashboardVo.builder()
                    .orderTotal(orderTotal)
                    .goodsTotal(goodsTotal)
                    .userTotal(userTotal)
                    .productTotal(productTotal)
                    .build();
            return dashboardVo;
        } catch (Exception e) {
           e.printStackTrace();
           return null;
        }
    }

    @Override
    public StatOrders statOrders() {
        StatOrders statOrders = new StatOrders();
        List<OrderStatInfo> orderStatInfo = statisticMapper.statOrders();
        statOrders.setRows(orderStatInfo);
        List<String> columns = new ArrayList<>();
        Collections.addAll(columns,"day","orders","customers","amount","pcr");
        /*Field[] fields = OrderStatInfo.class.getDeclaredFields();
        for (Field field : fields) {
            columns.add(field.getName());
        }*/
        statOrders.setColumns(columns);
        return statOrders;

    }

    @Override
    public StatGoods statGoods() {
        StatGoods statGoods = new StatGoods();
        List<StatGoodsRow> statGoodsRows = statisticMapper.statGoods();
        statGoods.setRows(statGoodsRows);
        List<String> columns = new ArrayList<>();
        Collections.addAll(columns,"day","orders","products","amount");
        statGoods.setColumns(columns);
        return statGoods;
    }

}
